<template>
  <view class="page-login">
    <view v-if="canIUse||canIGetUserProfile">
      <view class='content'>
        <image 
					style="width: 140rpx; height: 140rpx;" 
					mode="aspectFit" 
					src="../../static/logo_icon.png">
					</image>
        <view class="name">登录</view>
        <view>申请获取以下权限</view>
        <text>获得你的公开信息(昵称、头像、地区等)</text>
      </view>
      <view class="login-box">
        <!--新版登录方式-->
        <button 
					type="default"
					v-if="canIGetUserProfile"
					 class='login-btn'
					 @click="bindGetUserInfo"> 
					 授权登录
					</button>
        <!--旧版登录方式-->
        <button 
					v-else 
					type="default" 
					class='login-btn' 
					open-type="getUserInfo"
					withCredentials="true"
					lang="zh_CN" 
					@getuserinfo="bindGetUserInfo"> 
					授权登录 
					</button>

      </view>
    </view>
    <view v-else class="text-center">
      请升级微信版本
    </view>

  </view>
</template>
<script>
import * as loginUtils from '../../utils/loginUtils.js'
import * as userInfo from '../../common/api/userInfo.js'
export default {
  data () {
    return {
      canIUse: uni.canIUse('button.open-type.getUserInfo'),
      canIGetUserProfile: false,//微信登录新旧版本 true是新
			
    }
  },
  onLoad () {
		// loginUtils.login(()=>{})
    if (uni.getUserProfile) {
      this.canIGetUserProfile = true
    }
  },
  onShow () {

  },
  methods: {
    //登录授权
    bindGetUserInfo (e) {
      console.log('e', e)
      var _this = this
      if (this.canIGetUserProfile) {
        //新版登录方式
        uni.getUserProfile({
          desc: '获取您个人信息用于登录！',
          success: (res) => {
            console.log('用户信息', res)
            // 存入个人信息
            // uni.setStorageSync('userInfo_winxin', res.userInfo)
            _this.updateUserInfo(res.userInfo)
          },
          fail: (res) => {
            console.log(res)
          }
        })
      } else {
        //旧版登录方式 --自动就拉起授权窗口
        if (e.detail.userInfo) {
          //用户按了允许授权按钮
          //console.log('手动');
          console.log('老版用户信息', e.detail.userInfo)
          // 存入个人信息
          // uni.setStorageSync('userInfo_winxin', e.detail.userInfo)
          _this.updateUserInfo(e.detail.userInfo)
        } else {
          console.log('用户拒绝了授权')
          //用户按了拒绝按钮
        }
      }
    },
    //向后台添加信息
    updateUserInfo (info) {
      const params = {
        wxOpenid: uni.getStorageSync('openid_key'),
        nickname: info.nickName,
        headPortrait: info.avatarUrl,
        sex: ['未知','男','女'][info.gender],
      }
			console.log('updateUserInfo',params);
			userInfo.insert(params).then(result => {
				// 跳转到首页
				uni.reLaunch({ url: '/patientAppPages/pages/index' })
			}).catch(error => {
				console.log(error);
			})
 

    }
  }
}
</script>
<style>
	page{
		background: white;
		width: 100%;
		height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
<style lang="less" scoped>
.page-login{
	padding: 30rpx;
	box-sizing: border-box;
	.login-btn{
		border: none;
		background: #0BC5AE;
		color: white;
		border-radius: 48rpx;
		font-size: 30rpx;
		margin-top: 20rpx;
	}
}
.content {
  text-align: center;
}
</style>